package com.shopper.manager.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shopper.manager.config.ConfigCode;
import com.shopper.manager.config.RequestCode;
import com.shopper.manager.config.RequestMassage;
import com.shopper.manager.mapper.IndexMapper;
import com.shopper.manager.model.BaseBean;
import com.shopper.manager.model.index.*;
import com.shopper.manager.model.index.req.*;
import com.shopper.manager.model.index.result.CardResult;
import com.shopper.manager.model.index.result.EmployeesAttendanceResult;
import com.shopper.manager.model.index.result.EmployeesClockState;
import com.shopper.manager.model.index.result.LoginSuccessBean;
import com.shopper.manager.model.order.req.NormsInsertReq;
import com.shopper.manager.service.TokenService;
import com.shopper.manager.until.MdProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class IndexController extends BaseController {

    @Autowired
    private IndexMapper personMapper;

    @Autowired
    private TokenService tokenService;






    @PostMapping("s1/index/insertEmployees")
    public String insertEmployees(@RequestBody List<EmployeesIdBean> list) throws JsonProcessingException {
        int count = personMapper.insertListEmployees(list);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(new BaseBean(count, "插入职称成功", null));

    }


    @PostMapping("s1/index/registered")
    public String registered(@RequestBody RegisteredBean bean) throws JsonProcessingException {
        if (bean != null) {
            String pwd = MdProcess.md5(bean.getPassword());
            bean.setPassword(pwd);
            RegisteredBean registeredState = baseMapper.selectByAccount(bean.getAccount());
            if (registeredState == null) {
                personMapper.registered(bean);
                RegisteredBean registeredBean = baseMapper.selectByAccount(bean.getAccount());
                UserBean userBean = new UserBean();
                userBean.setAccount(registeredBean.getAccount());
                userBean.setUserId(registeredBean.getId());
                userBean.setBusinessLicense(registeredBean.getBusinessLicense());
                userBean.setLevel(bean.getLevel());
                String token=tokenService.getToken(userBean);
                userBean.setToken(token);
                personMapper.insertUser(userBean);
                LoginSuccessBean successBean = new LoginSuccessBean();
                successBean.setAccount(registeredBean.getAccount());
                successBean.setUserName(successBean.getUserName());
                successBean.setUserId(registeredBean.getId());
                successBean.setToken(token);
                return getsBaseBean(RequestCode.SUCCESS_CODE, RequestMassage.REGISTERED_SUCCESS, successBean);
            }

            return getsBaseBean(RequestCode.ERROR_ACCOUNT_REPEAT, RequestMassage.ACCOUNT_REPEAT, null);
        }


        return getsBaseBean(RequestCode.ERROR_REGISTERED_FAILED, RequestMassage.REGISTERED_FAILED, null);
    }


    @PostMapping("s1/index/employees/registered")
    public String employeesRegistered(@RequestHeader Map<String, String> header, @RequestBody RegisteredBean bean) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean managerBean = baseMapper.selectManagerAccount(Integer.valueOf(getTokenCode(header.get(ConfigCode.HEADER_TOKEN_KEY)).get(0)));
        if (managerBean == null) {
            return getsBaseBean(RequestCode.ERROR_NOT_PERMISSIONS, RequestMassage.NOT_FIND_PERMISSIONS, null);
        }
        if (bean != null) {
            String pwd = MdProcess.md5(bean.getPassword());
            bean.setPassword(pwd);
            RegisteredBean registeredState = baseMapper.selectByAccount(bean.getAccount());
            if (registeredState == null) {
                bean.setBusinessLicense(managerBean.getBusinessLicense());
                personMapper.registered(bean);
                RegisteredBean registeredBean = baseMapper.selectByAccount(bean.getAccount());
                UserBean userBean = new UserBean();
                userBean.setAccount(registeredBean.getAccount());
                userBean.setUserId(registeredBean.getId());
                userBean.setLevel(bean.getLevel());
                userBean.setShopAddressId(managerBean.getShopAddressId());
                userBean.setBusinessLicense(managerBean.getBusinessLicense());
                personMapper.insertUser(userBean);

                UserResultBean loginResultBean = new UserResultBean();
                loginResultBean.setAccount(registeredBean.getAccount());
                loginResultBean.setUserId(registeredBean.getId());

                return getsBaseBean(RequestCode.SUCCESS_CODE, RequestMassage.REGISTERED_SUCCESS, loginResultBean);
            }

            return getsBaseBean(RequestCode.ERROR_ACCOUNT_REPEAT, RequestMassage.ACCOUNT_REPEAT, null);
        }


        return getsBaseBean(RequestCode.ERROR_REGISTERED_FAILED, RequestMassage.REGISTERED_FAILED, null);
    }

    @PostMapping("s1/index/updateUserInfo")
    public String updateUserInfo(@RequestHeader Map<String, String> header, @RequestBody UserInfoBean bean) throws JsonProcessingException {

        int count = RequestCode.ERROR_UPDATE_FAILED;
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if ( bean != null) {
            int uid = Integer.valueOf(getTokenCode(header.get(ConfigCode.HEADER_TOKEN_KEY)).get(0));
            UserBean userBean = baseMapper.selectUserById(uid);
            if (userBean==null){
                return returnResultInfo(RequestCode.ERROR_ACCOUNT_REPEAT);
            }
            switch (bean.getType()){
                case 1://更新头像
                    userBean.setHeaderImage(bean.getContent());
                    break;
                case 2://更新昵称
                    userBean.setUserName(bean.getContent());
                    break;
                case 3://更新年龄
                    if (isNumeric(bean.getContent())){
                        userBean.setAge(Integer.valueOf(bean.getContent()));
                    }
                    break;
                case 4://更新地址
                    userBean.setAddress(bean.getContent());
                    break;
            }
            count = personMapper.updateUserInfo(userBean);
        }
        return getsBaseBean(count != 1 ? RequestCode.ERROR_UPDATE_FAILED : RequestCode.SUCCESS_CODE, count != 1 ? RequestMassage.UPDATE_DATA_FAILED : RequestMassage.UPDATE_DATA_SUCCESS, null);
    }

    @PostMapping("s1/index/login")
    public String login(@RequestBody RegisteredBean bean) throws JsonProcessingException {

        RegisteredBean registeredBean = personMapper.login(bean);
        if (registeredBean != null) {
            boolean isLogin = MdProcess.verify(bean.getPassword(), registeredBean.getPassword());
            UserBean userBean = baseMapper.selectUserById(registeredBean.getId());
            LoginSuccessBean successBean = new LoginSuccessBean();
            successBean.setAccount(registeredBean.getAccount());
            successBean.setUserName(successBean.getUserName());
            successBean.setUserId(registeredBean.getId());
            String token=tokenService.getToken(userBean);
            userBean.setToken(token);
            personMapper.updateToken(userBean);
            successBean.setToken(token);
            return getsBaseBean(isLogin ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_PASSWORD, isLogin ? RequestMassage.LOGIN_SUCCESS : RequestMassage.PASSWORD_ERROR, isLogin ? successBean : null);
        }
        return getsBaseBean(RequestCode.ERROR_SELECT_FAILED, RequestMassage.ACCOUNT_NOT_FIND, null);
    }

    @PostMapping("s1/index/Verification")
    public String VerificationCode(@RequestBody RegisteredBean bean) throws JsonProcessingException {

        RegisteredBean registeredBean = personMapper.login(bean);
        return returnResultInfo(registeredBean!=null?RequestCode.SUCCESS_CODE:RequestCode.ERROR_ACCOUNT_REPEAT);
    }

    @PostMapping("s1/index/VerificationCodeLogin")
    public String VerificationCodeLogin(@RequestBody RegisteredBean bean) throws JsonProcessingException {
        RegisteredBean registeredBean = personMapper.login(bean);
        if (registeredBean != null&&bean.getVerificationCode()==123456) {
            UserBean userBean = baseMapper.selectUserById(registeredBean.getId());
            LoginSuccessBean successBean = new LoginSuccessBean();
            successBean.setAccount(registeredBean.getAccount());
            successBean.setUserName(successBean.getUserName());
            successBean.setUserId(registeredBean.getId());
            String token=tokenService.getToken(userBean);
            userBean.setToken(token);
           int code= personMapper.updateToken(userBean);
            successBean.setToken(token);
            return returnResultInfo( RequestCode.SUCCESS_CODE ,  successBean);
        }
        return getsBaseBean(RequestCode.ERROR_SELECT_FAILED, RequestMassage.ACCOUNT_NOT_FIND, null);
    }


    @PostMapping("s1/index/getUserInfo")
    public String getUserInfo(@RequestHeader Map<String, String> header) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

            UserBean userBean = baseMapper.selectUserById(getUid(header));
            UserResultBean loginResultBean = new UserResultBean();
            loginResultBean.setAccount(userBean.getAccount());
            loginResultBean.setUserId(userBean.getId());
            loginResultBean.setAge(userBean.getAge());
            loginResultBean.setUserName(userBean.getUserName());
            loginResultBean.setAddress(userBean.getAddress());
            loginResultBean.setHeaderImage(userBean.getHeaderImage());
            EmployeesIdBean eb = personMapper.selectEmployees(userBean.getLevel());
            if (eb != null) {
                UserResultBean.Employees employees = new UserResultBean.Employees();
                employees.setEmployeesType(eb.getEmployeesType());
                employees.setContent(eb.getContent());
                employees.setLevel(eb.getLevel());
                employees.setNickName(eb.getNickName());
                loginResultBean.setEmployees(employees);
            }
            ShopAddressBean sab = personMapper.selectShopAddress(userBean.getShopAddressId());
            if (sab != null) {
                UserResultBean.ShopAddress sAddress = new UserResultBean.ShopAddress();
                sAddress.setId(sab.getId());
                sAddress.setLatitude(sab.getLatitude());
                sAddress.setLongitude(sab.getLongitude());
                sAddress.setShopAddress(sab.getShopAddress());
                loginResultBean.setShopAddress(sAddress);
            }
        InsertClockTimeReq clockTimeReq=personMapper.selectBusinessClockTime(userBean.getBusinessLicense());
            if (clockTimeReq!=null){
                UserResultBean.ClockTime clockTime=new UserResultBean.ClockTime();
                clockTime.setEndTime(clockTimeReq.getEndTime());
                clockTime.setStartTime(clockTimeReq.getStartTime());
                loginResultBean.setClickTime(clockTime);
            }
            return returnResultInfo( RequestCode.SUCCESS_CODE,loginResultBean);

    }


    @PostMapping("s1/index/updatePassword")
    public String updatePassword(@RequestHeader Map<String, String> header,@RequestBody RegisteredBean bean) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        int uid = Integer.valueOf(getTokenCode(header.get(ConfigCode.HEADER_TOKEN_KEY)).get(0));
        UserBean userBean = baseMapper.selectUserById(uid);

        if (userBean != null) {
            if (!userBean.getAccount().equals(bean.getAccount())){
                return getsBaseBean(RequestCode.ERROR_UPDATE_ACCOUNT, RequestMassage.UPDATE_ACCOUNT_ERROR, null);
            }
            RegisteredBean registeredBean=personMapper.login(bean);
            boolean isLogin = MdProcess.verify(bean.getStartPwd(), registeredBean.getPassword());
            if (isLogin) {
                bean.setPassword(MdProcess.md5(bean.getPassword()));
                int result = personMapper.updatePassword(bean);
                return getsBaseBean(result == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED, result == 1 ? RequestMassage.UPDATE_PASSWORD_SUCCESS : RequestMassage.UPDATE_PASSWORD_FAILED, null);
            } else {
                return getsBaseBean(RequestCode.ERROR_PASSWORD, RequestMassage.ORIGINAL_PASSWORD_ERROR, null);
            }

        } else {
            return getsBaseBean(RequestCode.ERROR_ACCOUNT_REPEAT, RequestMassage.ACCOUNT_NOT_FIND, null);
        }

    }


    @PostMapping("s1/index/forgetPassword")
    public String forgetPassword(@RequestBody RegisteredBean bean) throws JsonProcessingException {

        RegisteredBean registeredBean = personMapper.login(bean);
        if (registeredBean != null) {
            if (!bean.getStartPwd().equals(bean.getEndPwd())){
                return  getsBaseBean(RequestCode.ERROR_PASSWORD, RequestMassage.PASSWORD_ERROR, null);
            }
            if (registeredBean.getBusinessLicense().equals(bean.getBusinessLicense()))
            {
                bean.setPassword(MdProcess.md5(bean.getStartPwd()));
                int result = personMapper.forgetPassword(bean);
                return getsBaseBean(result == 1 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED, result == 1 ? RequestMassage.UPDATE_PASSWORD_SUCCESS : RequestMassage.UPDATE_PASSWORD_FAILED, null);
            } else {
                return getsBaseBean(RequestCode.ERROR_BUSINESS_LICENSE, RequestMassage.BUSINESS_LICENSE_ERROR, null);
            }

        } else {
            return getsBaseBean(RequestCode.ERROR_ACCOUNT_REPEAT, RequestMassage.ACCOUNT_NOT_FIND, null);
        }

    }


    @PostMapping("s1/index/selectListEmployees")
    public String selectListEmployees(@RequestHeader Map<String, String> header, @RequestBody UserBean bean) throws JsonProcessingException {


        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        int uid = Integer.valueOf(getTokenCode(header.get(ConfigCode.HEADER_TOKEN_KEY)).get(0));
        bean.setUserId(uid);
        UserBean userBean = baseMapper.selectManagerAccount(uid);
        if (userBean != null) {
            List<UserBean> list = personMapper.selectListEmployees(userBean);
            if (list != null && list.size() > 0) {
                return getsBaseBean(RequestCode.SUCCESS_CODE, RequestMassage.REQUEST_SUCCESS, list);
            } else {
                return getsBaseBean(RequestCode.ERROR_DATA_NULL, RequestMassage.DATA_NULL, null);
            }

        }


        return getsBaseBean(RequestCode.ERROR_NOT_PERMISSIONS, RequestMassage.NOT_FIND_PERMISSIONS, null);

    }


    @PostMapping("s1/index/deleteEmployees")
    public String deleteEmployees(@RequestHeader Map<String, String> header, @RequestBody UserBean bean) throws JsonProcessingException {


        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        int uid = Integer.valueOf(getTokenCode(header.get(ConfigCode.HEADER_TOKEN_KEY)).get(0));
        UserBean userBean = baseMapper.selectManagerAccount(uid);
        if (userBean != null) {
            int result = personMapper.deleteEmployees(bean);
            return getsBaseBean(result > 0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UNKNOWN, result > 0 ? RequestMassage.DELETE_ACCOUNT_SUCCESS : RequestMassage.UNKNOWN_ERROR, null);
        }

        return getsBaseBean(RequestCode.ERROR_NOT_PERMISSIONS, RequestMassage.NOT_FIND_PERMISSIONS, null);

    }


    @PostMapping("s1/index/insertShopAddress")
    public String insertShopAddress(@RequestHeader Map<String, String> header, @RequestBody ShopAddressBean bean) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        int uid = Integer.valueOf(getTokenCode(header.get(ConfigCode.HEADER_TOKEN_KEY)).get(0));

        UserBean userBean = baseMapper.selectManagerAccount(uid);
        if (userBean != null) {
            bean.setBusinessLicense(userBean.getBusinessLicense());
            bean.setTime(getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            bean.setEditAuthor(userBean.getAccount());
            if (bean.getType()==1){
                int result = personMapper.insertShopAddress(bean);
                int updateResult = personMapper.updateUserShopAddress();
                if (result > 0 && updateResult > 0) {
                    return returnResultInfo(RequestCode.SUCCESS_CODE);

                }
            }else {
                int result=personMapper.updateShopAddress(bean);
                return returnResultInfo(result>0?RequestCode.SUCCESS_CODE:RequestCode.ERROR_UPDATE_FAILED);
            }



            return  returnResultInfo(RequestCode.ERROR_UPDATE_FAILED);
        }

        return returnResultInfo(RequestCode.ERROR_NOT_PERMISSIONS);
    }



    @PostMapping("s1/index/loginOut")
    public String loginOut(@RequestHeader Map<String, String> header) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        userBean.setToken("");
        int isOut = personMapper.deleteToken(userBean);
        return returnResultInfo(isOut>0?RequestCode.SUCCESS_CODE:RequestCode.ERROR_UNKNOWN);

    }



    @PostMapping("s1/index/selectEmployeeLevel")
    public String selectEmployeeLevel(@RequestHeader Map<String, String> header) throws JsonProcessingException {


        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        List<EmployeesIdBean> list=personMapper.selectEmployeeLevel();
        boolean isTrue=list!=null&&list.size()>0;

      return   returnResultInfo(isTrue?RequestCode.SUCCESS_CODE:RequestCode.ERROR_SELECT_FAILED,isTrue?list:null);

    }


    /**
     * 上班打卡
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/clockGoWork")
    public String clockGoWork(@RequestHeader Map<String, String> header, @RequestBody EmployeesClockReq req) throws JsonProcessingException {


        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        req.setUserAccount(userBean.getAccount());
        req.setClockDate(getYearMonthDay(req.getClockTime()));
        int code=0;
        InsertClockTimeReq clockTime=personMapper.selectBusinessClockTime(userBean.getBusinessLicense());
        switch (req.getClockState()){
            case "1":
                code=personMapper.clockGoWork(req);
                break;
            case "2":
                EmployeesClockState data=personMapper.getClockState(req);
                String sTime="";
                if (data!=null){
                    sTime=data.getClockTime();//上班卡
                }
               String eTime=req.getClockTime();//下班卡
                if (!isEmpty(sTime)&&!isEmpty(eTime)){
                    if (isClickTime(sTime,clockTime.getStartTime())&&isAfterTime(eTime,clockTime.getEndTime())){
                        req.setClockType(1);
                    }else {
                        req.setClockType(1003);
                    }
                }else if (isEmpty(sTime)&&isEmpty(eTime)){
                    req.setClockType(0);
                }else {
                    req.setClockType(1003);
                }


                  int count=personMapper.isClockWork(req);
                  if (count>0){
                      code=personMapper.updateAfterWork(req);
                  }else {
                      code=personMapper.clockAfterWork(req);
                  }

                break;
        }



        return   returnResultInfo(code>0?RequestCode.SUCCESS_CODE:RequestCode.ERROR_UPDATE_FAILED);

    }

    /**
     * 是否超过打卡时间
     */
    private boolean isClickTime( String time,String clockTime ) {

        try {
          Date dateF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateF);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);


            Date cDate = new SimpleDateFormat("HH:mm").parse(clockTime);
            Calendar cCalendar = Calendar.getInstance();
            cCalendar.setTime(cDate);
            int cHour=cCalendar.get(Calendar.HOUR_OF_DAY);
            int cMinute=cCalendar.get(Calendar.MINUTE);

            if (hour > cHour) {
                return false;
            }else if (hour==cHour&&minute > cMinute){
                 return false;
            }
            return true;
        } catch (ParseException e) {

            e.printStackTrace();

            return false;
        }

    }


    /**
     * 是否提前打下班卡
     */
    private boolean isAfterTime( String time,String clockTime ) {

        try {
            Date dateF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateF);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);

            Date cDate = new SimpleDateFormat("HH:mm").parse(clockTime);
            Calendar cCalendar = Calendar.getInstance();
            cCalendar.setTime(cDate);
            int cHour=cCalendar.get(Calendar.HOUR_OF_DAY);
            int cMinute=cCalendar.get(Calendar.MINUTE);

            if (cHour > hour) {
                return false;
            }else if (cHour==hour&&minute<cMinute){
                return false;
            }
            return true;
        } catch (ParseException e) {

            e.printStackTrace();

            return false;
        }

    }


    /**
     * 获取今天打卡状态
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/getClockState")
    public String getClockState(@RequestHeader Map<String, String> header, @RequestBody EmployeesClockReq req) throws JsonProcessingException {


        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        req.setUserAccount(userBean.getAccount());
        req.setClockDate(getYearMonthDay(req.getClockTime()));
        EmployeesClockState data=personMapper.getClockState(req);


        return   returnResultInfo(RequestCode.SUCCESS_CODE,data);

    }


    /**
     * 获取员工打卡考勤
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/getEmployeesAttendance")
    public String getEmployeesAttendance(@RequestHeader Map<String, String> header, @RequestBody EmployeesAttendanceReq req) throws JsonProcessingException {


        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        if (isEmpty(req.getUserAccount())){
            req.setUserAccount(userBean.getAccount());
        }
        List<EmployeesAttendanceResult> data=personMapper.getEmployeesAttendance(req);


        return   returnResultInfo(RequestCode.SUCCESS_CODE,data);

    }


    /**
     * 提交缺勤
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/commitCard")
    public String commitCard(@RequestHeader Map<String, String> header,@RequestBody CardReq req) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));

        EmployeesClockReq clockReq=new EmployeesClockReq();
        clockReq.setUserAccount(userBean.getAccount());
        clockReq.setClockDate(req.getAbsenteeismTime());
        clockReq.setBusinessLicense(userBean.getBusinessLicense());

        List<CardResult>  list=personMapper.getReqCardList(clockReq);
        if (list!=null&&list.size()>0){
            return   returnResultInfo(RequestCode.ERROR_RESET_COMMIT);
        }

        req.setBusinessLicense(userBean.getBusinessLicense());
        req.setAccount(userBean.getAccount());
        req.setCardTime(getCurrentDate("yyyy-MM-dd HH:mm:ss"));
        req.setCardState(0);
       int code=personMapper.commitCard(req);

        return   returnResultInfo(code>0?RequestCode.SUCCESS_CODE:RequestCode.ERROR_UPDATE_FAILED);

    }


    /**
     * 获得员工缺勤列表
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/getCardList")
    public String getCardList(@RequestHeader Map<String, String> header) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

            CardReq    req=new CardReq();
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        if (userBean.getLevel()<=2){
            req.setBusinessLicense(userBean.getBusinessLicense());
            req.setApprover(userBean.getAccount());
            req.setBusinessLicense(userBean.getBusinessLicense());
            List<CardResult> list=  personMapper.getCardList(req);

            return returnResultInfo(RequestCode.SUCCESS_CODE,list);
        }else {
            return   returnResultInfo(RequestCode.ERROR_NOT_PERMISSIONS);
        }


    }


    /**
     * 删除员工考勤
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/deleteCard")
    public String deleteCard(@RequestHeader Map<String, String> header,@RequestBody CardReq req) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        if (userBean.getLevel()<=2){

            int code=  personMapper.deleteCard(req);

            return returnResultInfo(code>0?RequestCode.SUCCESS_CODE:RequestCode.ERROR_UPDATE_FAILED);
        }else {
            return   returnResultInfo(RequestCode.ERROR_NOT_PERMISSIONS);
        }


    }


    /**
     * 确认员工补卡
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/configCard")
    public String configCard(@RequestHeader Map<String, String> header,@RequestBody CardReq req) throws JsonProcessingException {

        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));

        if (userBean.getLevel()<=2){

            EmployeesClockReq clockReq=new EmployeesClockReq();
            clockReq.setUserAccount(req.getAccount());
            clockReq.setClockDate(req.getAbsenteeismTime());
            clockReq.setBusinessLicense(userBean.getBusinessLicense());
            clockReq.setClockType(2);
            EmployeesAttendanceResult  attendanceResult=personMapper.getEmployeesDate(clockReq);
            int state=0;
            if (attendanceResult!=null){

                 state= personMapper.updateCommitClockState(clockReq);
            }else {
               state=personMapper.insertCardState(clockReq);
            }

            if (state>0){
                req.setCardState(1);
                int code=  personMapper.configCard(req);
                return returnResultInfo(code>0?RequestCode.SUCCESS_CODE:RequestCode.ERROR_UPDATE_FAILED);
            }


            return returnResultInfo(RequestCode.ERROR_UPDATE_FAILED);
        }else {
            return   returnResultInfo(RequestCode.ERROR_NOT_PERMISSIONS);
        }


    }

    @PostMapping("s1/index/uploadAvatar")
    public String uploadAvatar(@RequestParam("file") MultipartFile file) throws JsonProcessingException {
//判断token的代码就省略了
        if(file.isEmpty()){
            return returnResultInfo(RequestCode.ERROR_FILE_NOT_EXIST);
        }
        String fileName = file.getOriginalFilename();//文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
        String path = "E:/workspace/JavaEEWeb/tsemap-parent/tsemap-user/src/main/resources/static/img"; //文件存储位置  我放在了我的项目下
        fileName= UUID.randomUUID()+suffixName;//图片名
        File dest = new File(path+"/"+fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            String url="/image/"+fileName;
            Map map=new HashMap();
            map.put("url",url);
            return returnResultInfo(RequestCode.SUCCESS_CODE,url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return returnResultInfo(RequestCode.ERROR_UPDATE_FAILED);
    }

    /**
     * 创建打卡时间
     * @param header
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("s1/index/insertClockTime")
    public String insertClockTime(@RequestHeader Map<String, String> header, @RequestBody InsertClockTimeReq req) throws JsonProcessingException {
        String token = processToken(header);
        if (!token.equals(ConfigCode.TOKEN_OK)) {
            return token;
        }

        if (req == null) {
            return returnResultInfo(RequestCode.ERROR_UPLOAD_CODE);
        }
        UserBean userBean = baseMapper.selectUserById(getUid(header));
        req.setBusinessLicense(userBean.getBusinessLicense());
        req.setCreatTime(""+System.currentTimeMillis());
        InsertClockTimeReq clockTimeReq=personMapper.selectBusinessClockTime(userBean.getBusinessLicense());
        if (clockTimeReq!=null){
            int code=personMapper.updateClockTime(req);
            return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,code >0?req:null);
        }else {
            int code = personMapper.insertCreateClockTime(req);
            return returnResultInfo(code >0 ? RequestCode.SUCCESS_CODE : RequestCode.ERROR_UPDATE_FAILED,code >0?req:null);
        }


    }
}
